package com.fjrcloud.service;

import com.fjrcloud.dao.RelocationTransitionDao;
import com.fjrcloud.entity.*;
import com.fjrcloud.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by shengjian.lin on 17-2-15.
 */
@Service
public class RelocationTransitionService {

    @Autowired
    private HouseholdService householdService;
    @Autowired
    private IdentifiedHousingService identifiedHousingService;

    @Autowired
    private RelocationTransitionDao dao;

    public RelocationTransition get(HouseholdMaster master){
        return dao.findByMasterAndValid(master,true);
    }

    public void del(Integer id){
        dao.delete(id);
    }

    public double getCompensateArea(Integer masterId) throws BusinessException {
        Page<IdentifiedHousing> page = identifiedHousingService.find(masterId,null,null);
        List<IdentifiedHousing> list = page.getContent();

        double compensate = 0.0;
        for (IdentifiedHousing housing:list) {
            if(housing.getLandStandard().getPattern()){
                 double _compensate = housing.getArea()*housing.getLandStandard().getRatio()/100.0;
                if (Formalities.without.equals(housing.getLandStandard().getFormalities()) && _compensate > 150)
                    _compensate = 150;
                if (housing.getLayer() > 0 && true == housing.isAtticHighly())
                    _compensate = 0;
                if (housing.getLayer() < 0 && Basement.Third2MoreThanHalf.equals(housing.getBasement()))
                    _compensate *= 0.5;
                if (housing.getLayer() < 0 && Basement.LessThanThird.equals(housing.getBasement()))
                    _compensate = 0;
                compensate += _compensate;
            }
        }

        return compensate;
    }

    public RelocationTransition save(RelocationTransition entity, Integer masterId) throws BusinessException {

        if (masterId == null)
            throw new BusinessException("主体错误");
        HouseholdMaster master = householdService.getMaster(masterId);
        if (master == null)
            throw new BusinessException("主体错误");
        RelocationTransition old = get(master);

        if (entity.getCurrencyCompensateArea() == null)
            entity.setCurrencyCompensateArea(0.0);
        if (entity.getExistingCompensateArea() == null)
            entity.setExistingCompensateArea(0.0);
        if (entity.getFirstPhaseCompensateArea() == null)
            entity.setFirstPhaseCompensateArea(0.0);
        if (entity.getSecondPhaseCompensateArea() == null)
            entity.setSecondPhaseCompensateArea(0.0);

        double total = entity.getCurrencyCompensateArea()+entity.getExistingCompensateArea()+entity.getFirstPhaseCompensateArea()+entity.getSecondPhaseCompensateArea()-getCompensateArea(masterId);
        if(total<0)
            throw new BusinessException("补偿面积总和小于认定补偿面积，还剩下："+Math.abs(total));
        if(total>0)
            throw new BusinessException("补偿面积总和大于认定补偿面积，还多出："+Math.abs(total));


        if (old != null){
            entity.setId(old.getId());
        }
        entity.setMaster(master);

        return dao.save(entity);

    }

}
